# Copyright (c) 2006 "Gardiner"
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'util'

class StrainTendings
	attr :strainName
	attr :description
	attr :tendings

	def initialize(name, desc)
		@strainName = name
		@description = desc
		@tendings = Array.new(WineStrings.states.length) { |t|
			t = Array.new(WineStrings.tendings.length, 0)
		}
	end
	
	def add_tending(state, tending, acid, color, grapes, quality, skin, sugar, vigor)
		stateNum = WineStrings.get_state(state.downcase.capitalize)
		tendingNum = WineStrings.get_tending(tending.downcase.capitalize)
	
		if !stateNum
			raise "Bad state \"#{state}\""
		elsif !tendingNum && state.downcase != 'initial'
			raise "Bad tending \"#{tending}\""
		end
		
		# The 'initial' state isn't really a state - we just use it to store the initial values
		if state.downcase == 'initial'
			@tendings[stateNum].length.times { |i|
				@tendings[stateNum][i] = [acid, color, grapes, quality, skin, sugar, vigor]
			}
		else
			@tendings[stateNum][tendingNum] = [acid, color, grapes, quality, skin, sugar, vigor]
		end
		
	end
	
	def to_s
		str = "#{strainName} (#{description})\n"
		tendings.each_with_index { |state, i|
			str << "\n#{WineStrings.states[i]}: \n"
			
			state.each_with_index { |t, j|
				str << "#{WineStrings.tendings[j]}:\t\t#{t.join(", ")}\n"
			}
		}
		
		str
	end
end

class Vineyard
	attr :vineyardName
	attr :description
	attr :states
	
	def initialize(name, desc)
		@vineyardName = name
		@description = desc
		@states = []
	end
	
	def add_state(state)
		stateNum = WineStrings.get_state(state.downcase.capitalize)
		
		if !stateNum
			raise "Bad state \"#{state}\""
		end
		
		@states << stateNum
	end
	
	def to_s
		str = "#{vineyardName} (#{description})\n\n"
		states.each { |s|
			str << "#{WineStrings.states[s]}\n"		
		}
		
		str
	end
end

class WineStrings
	@@states = ['Initial',
		   'Vines are sagging a bit', 
		   'Leaves are wilting',
		   'A musty smell can be detected',
		   'Stems look especially fat',
		   'Leaves rustle in the breeze',
		   'Grapes are starting to shrivel',
		   'Leaves shimmer with moisture']
	@@shortStates = ['Initial',
			  'Sagging',
			  'Wilting',
			  'Musty',
			  'Fat',
			  'Rustle',
			  'Shrivel',
			  'Shimmer']
	@@tendings = ['Aerate the soil',
		      'Mist the grapes',
		      'Pinch off weakest stems',
		      'Shade the leaves',
		      'Spread out the vines',
		      'Tie the vines to the trellis',
		      'Trim the lower leaves']
	@@shortTendings = ['AS',
			     'MG',
			     'PO',
			     'SL',
			     'SV',
			     'TV',
			     'TL']
	@@stats = ['Acid',
		   'Color',
		   'Grapes',
		   'Quality',
		   'Skin',
		   'Sugar',
		   'Vigor']
		   
	def WineStrings.get_state(stateStr)
		WineStrings.states.index(stateStr.downcase.capitalize) or WineStrings.shortStates.index(stateStr.downcase.capitalize)
	end
	
	def WineStrings.get_tending(tendingStr)
		WineStrings.tendings.index(tendingStr.downcase.capitalize) or WineStrings.shortTendings.index(tendingStr.upcase)
	end

	def WineStrings.states
		@@states
	end
	
	def WineStrings.shortStates
		@@shortStates
	end
	
	def WineStrings.tendings
		@@tendings
	end
	
	def WineStrings.shortTendings
		@@shortTendings
	end
	
	def WineStrings.stats
		@@stats
	end
end